package com.study.zouchao.superproject.homepage.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.search.busline.BusLineResult;
import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.ZouImgLoader;
import com.study.zouchao.superproject.base_zou.api_zou.Api_C;
import com.study.zouchao.superproject.homepage.model.result.entity.BaiduNewResult;
import com.study.zouchao.superproject.newsdetail.NewDetailActivity;
import com.study.zouchao.superproject.util.App;
import com.study.zouchao.superproject.util.StringUtils;
import com.study.zouchao.superproject.util.ToastUtils;
import com.study.zouchao.superproject.web.WebActivity;

import org.jsoup.Connection;

import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class NewsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int TYPE_WITHOUT_PIC = 0;
    private final int TYPE_WITH_PIC = 1;
    private final int TYPE_FOOTER = 2;
    private View mFooterView;

    private List<BaiduNewResult.DataBean.ListBean> mDatas;

    private Context mContext;

    public NewsListAdapter(Context context, List<BaiduNewResult.DataBean.ListBean> data) {
        mDatas = data;
        mContext = context;
    }

    public void setFooterView(View footerView) {
//        mFooterView = footerView;
//        notifyItemInserted(getItemCount() - 1);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType==TYPE_FOOTER && mFooterView!=null)
            return new NewItemHolder(mFooterView);
        if (viewType==TYPE_WITH_PIC)
            return new NewItemWithPic(
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_with_pic, parent, false));
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_without_pic, parent, false);
        return new NewItemHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (position < 0 || mDatas.size()<0)   return;
        final BaiduNewResult.DataBean.ListBean bean = mDatas.get(position);
        if(holder instanceof NewItemHolder){
            ((NewItemHolder) holder).tvTitle.setText(bean.getTitle()+"");
            ((NewItemHolder) holder).tvTime.setText(bean.getPublicTime()+"");
            ((NewItemHolder) holder).rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!StringUtils.isEmpty(bean.getUrl()))
//                        WebActivity.actionStartWebActivity(mContext, bean.getUrl());
                    NewDetailActivity.actionStartNewDetailActivity(mContext, bean.getUrl());
                    else ToastUtils.showShort(mContext, "查看详情失败");
                    ToastUtils.showShort(App.getAppContext(), "敬请期待~~");

                }
            });
        }
        if(holder instanceof NewItemWithPic){
            ((NewItemWithPic) holder).tvTitle.setText(bean.getTitle()+"");
            ((NewItemWithPic) holder).tvTime.setText(bean.getPublicTime()+"");
            ZouImgLoader.loadImage(mContext, ((NewItemWithPic) holder).iv, bean.getImgUrl(), R.drawable.error);
            ((NewItemWithPic) holder).rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!StringUtils.isEmpty(bean.getUrl()))
//                        WebActivity.actionStartWebActivity(mContext, bean.getUrl());
                        NewDetailActivity.actionStartNewDetailActivity(mContext, bean.getUrl());
                    else ToastUtils.showShort(mContext, "查看详情失败");
                    ToastUtils.showShort(App.getAppContext(), "敬请期待~~");
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if(mFooterView == null) return mDatas.size();
        else return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        //最后一个,应该加载Footer
//        if (position == (getItemCount()-1)) return TYPE_FOOTER;
        BaiduNewResult.DataBean.ListBean item = mDatas.get(position);
        if ( ! StringUtils.isEmpty(item.getImgUrl()))  return TYPE_WITH_PIC;
        return TYPE_WITHOUT_PIC;
    }
}


class NewItemHolder extends RecyclerView.ViewHolder {
    public TextView tvTitle, tvTime;
    public View rootView;

    public NewItemHolder(View rootView) {
        super(rootView);
        this.rootView = rootView;
        tvTitle = (TextView) rootView.findViewById(R.id.id_tv_title);
        tvTime = (TextView) rootView.findViewById(R.id.id_tv_time);
    }
}


class NewItemWithPic extends RecyclerView.ViewHolder {
    public View rootView;
    public ImageView iv;
    public TextView tvTitle, tvTime;

    public NewItemWithPic(View rootView) {
        super(rootView);
        this.rootView = rootView;
        iv = (ImageView) rootView.findViewById(R.id.id_iv);
        tvTitle = (TextView) rootView.findViewById(R.id.id_tv_title);
        tvTime = (TextView) rootView.findViewById(R.id.id_tv_time);
    }
}
